﻿using OA.IRepository;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OA.Repository;

namespace OA.Repository
{
    /// <summary>
    /// 数据会话层
    /// 作用：
    /// （1）实现事务（通过调用SaveChanges（）方法）
    /// （2）接触业务层和仓储层之间的耦合，利用当前类创建具体的仓储层对象
    /// 
    /// </summary>
    public class DbSession : IDbSession
    {

        //public OADbContext context=DbContextFactory.CreateContext();
        public OADbContext context
{
get
{
                return DbContextFactory.CreateContext();
            }
           
        }

        private ISysORgRepository sysOrgRepository;


        private ISysPersonRepository sysPersonRepository;

        public ISysPersonRepository SysPersonRepository
        {
            get 
            {
                if (sysPersonRepository == null)
                {
                    sysPersonRepository = new SysPersonRepository();
                }
                return sysPersonRepository;
            }
            set { SysPersonRepository= value; }

        }

        public ISysORgRepository SysORgRepository
        {
            get
            {
                if (sysOrgRepository == null)
                {
                    sysOrgRepository = new SysOrgRepository();
                }
                return sysOrgRepository;
            }
            set { sysOrgRepository = value; }
        }

        public int SaveChanges()
        {
            return context.SaveChanges();
        }
    }
}
